package sk.fiit.icp.data;

import java.util.Collections;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class RepetitionDaoImpl implements RepetitionDao {
	private HibernateTemplate hibernateTemplate;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	@Transactional(readOnly = false)
	public void persist(Repetition r) {
		hibernateTemplate.persist(r);
	}

	@Transactional
	public Repetition findById(int id) {
		@SuppressWarnings("unchecked")
		List<Repetition> repetitions = hibernateTemplate.find("from Repetition AS r WHERE r.idrepetition = ?", id);
		return (Repetition) (repetitions.isEmpty() ? Collections.emptyList() : repetitions.get(0));
	}

	@SuppressWarnings("unchecked")
	public List<Repetition> findAll() {
		return hibernateTemplate.find("from Repetition");
	}

	@Transactional
	public int remove(int id) {
		return hibernateTemplate.bulkUpdate("delete from Repetition where idrepetition = ?",id);
	}

	@Transactional(readOnly = false)
	public void update(Repetition r) {
		hibernateTemplate.update(r);
	}

}
